package com.example.txttoyy

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.TypedValue
import org.jdeferred.android.AndroidDeferredManager


object UiKit {
    private val gDM = AndroidDeferredManager()
    private val gUiHandler = Handler(Looper.getMainLooper())
    fun defer(): AndroidDeferredManager {
        return gDM
    }

    fun dpToPx(context: Context, dp: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(),
            context.resources.displayMetrics
        ).toInt()
    }

    fun post(r: Runnable?) {
        gUiHandler.post(r!!)
    }

    fun postDelayed(delay: Long, r: Runnable?) {
        gUiHandler.postDelayed(r!!, delay)
    }

    fun sleep(time: Long) {
        try {
            Thread.sleep(time)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

    fun sleep() {
        try {
            Thread.sleep(500)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }
}
